﻿using System;
namespace Stock.Core.Common
{
    public class TimeHelper
    {
		public static double ToTimestamp(DateTime value)
		{
			TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
			return (double)span.TotalSeconds;
           
		}

		public static DateTime ConvertTimestamp(double timestamp)
		{
			DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
			DateTime newDateTime = converted.AddSeconds(timestamp);
			return newDateTime.ToLocalTime();
		}

		/// <summary>  
		/// 时间戳转为C#格式时间  
		/// </summary>  
		/// <param name="timeStamp">Unix时间戳格式</param>  
		/// <returns>C#格式时间</returns>  
        public static DateTime GetTime(string timeStamp)
		{
            DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0), TimeZoneInfo.Local);
            long lTime = long.Parse(timeStamp + "0000");

			TimeSpan toNow = new TimeSpan(lTime);
			return dtStart.Add(toNow);
		}

		/// <summary>  
		/// DateTime时间格式转换为Unix时间戳格式  
		/// </summary>  
		/// <param name="time"> DateTime时间格式</param>  
		/// <returns>Unix时间戳格式</returns>  
        public static long ConvertDateTimeInt(System.DateTime time)
		{
			DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0), TimeZoneInfo.Local);
			TimeSpan toNow = time.Subtract(dtStart);
			long timeStamp = toNow.Ticks;
			timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
			return timeStamp;
		}
	}
}
